package DBLag;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import ModelLag.*;


public class DbIndlaeggelse {
	
	private Connection con;
	private DbPatient Dbpatient;
	private DbMedarbejder Dbmedarbejder;
	
	public DbIndlaeggelse() {
		con = DbConnection.getInstance().getDBcon();
		Dbpatient = new DbPatient();
		Dbmedarbejder = new DbMedarbejder();
	}
	
	public int tilfoejIndlaeggelse(Indlaeggelse i) {
		int rc = -1;
		PreparedStatement pstmt = null;
		String insert = "insert into Indlaeggelse(iId, pId, mId, IndlaeggelsesDato, IndlaeggelsesTidspunkt)" + "values (?,?,?,?,?)";
		System.out.println(insert);
		try {
			pstmt = con.prepareStatement(insert);
			pstmt.setInt(1, i.getiId());
			pstmt.setInt(2, i.getPatient().getpId());
			pstmt.setInt(3, i.getMedarbejder().getpId());
			pstmt.setString(4, i.getIndlaeggelsesDato());
			pstmt.setString(5, i.getIndlaeggelsesTidspunkt());

			rc = pstmt.executeUpdate();
		} catch (SQLException sqlE) {
			System.out.println(sqlE.getMessage());
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}

		return rc;
	}

	public int opdaterIndlaeggelse(Indlaeggelse i) {
		Indlaeggelse iObj = i;
		int rc = -1;
		String query = "UPDATE Indlaeggelse SET " + "mId ='" + iObj.getMedarbejder().getpId()
				+ "', " + "IndlaeggelsesDato ='"
				+ iObj.getIndlaeggelsesDato() + "', " + "IndlaeggelsesTidspunkt ='"
				+ iObj.getIndlaeggelsesTidspunkt() + "' "
				+ " WHERE iId = '"
				+ iObj.getiId() + "'";
		System.out.println("Update query:" + query);

		System.out.println(query);
		try{
			Statement stmt = con.createStatement();
			stmt.setQueryTimeout(5);
			rc = stmt.executeUpdate(query);
		}
		catch(Exception ex){
			System.out.println("Indlaeggelsen kunne ikke opdateres: " + ex);
		}
		
		return rc;
	}
	

	public int sletIndlaeggelse(int pId) {
		int rc = -1;
		PreparedStatement pstmt = null;
		String slet = "delete from Indlaeggelse where iId = ?";
		System.out.println("Sletter fra Indlaeggelsen" + slet);
		try{
			pstmt = con.prepareStatement(slet);
			pstmt.setInt(1, pId);
			rc = pstmt.executeUpdate();
			System.out.println("Indlaeggelsen er blevet slettet fra databasen");
		}
		catch(SQLException sqlE){
			System.out.println(sqlE.getMessage());
		}
		catch(Exception e){
			System.out.println(e.getMessage());
		}
		
		return rc;
	}
	
	public Indlaeggelse findIndlaeggelse(int iId) {
		String wClause = " Indlaeggelse.iId = '" + iId + "'";
		return singleWhere(wClause);
	}
	
	private Indlaeggelse singleWhere(String wClause){
		ResultSet results;
		Indlaeggelse i = new Indlaeggelse();
		
		String query = buildQuery(wClause);
		System.out.println(query);
		try{
			Statement stmt = con.createStatement();
			stmt.setQueryTimeout(5);
			results = stmt.executeQuery(query);
			
			if(results.next()){
				i = buildIndlaeggelse(results);
				stmt.close();
			} else {
				i = null;
			}
			
		}
		catch(Exception exe){
			
		}
		return i;
	}
	
	private String buildQuery(String wClause) {
		String query = "SELECT * FROM Indlaeggelse";
		if (wClause.length() > 0) {
			query = query + " WHERE " + wClause;
		}
		return query;
	}
	
	private Indlaeggelse buildIndlaeggelse(ResultSet results) {
		Indlaeggelse i = new Indlaeggelse();

		try {
			i.setiId(results.getInt("iId"));
			Person p = Dbpatient.findPatient(results.getInt("pId"));
			i.setPatient(p);
			Person m = Dbmedarbejder.findMedarbejder(results.getInt("mId"));
			i.setMedarbejder(m);
			i.setIndlaeggelsesDato(results.getString("IndlaeggelsesDato"));
			i.setIndlaeggelsesTidspunkt(results.getString("IndlaeggelsesTidspunkt"));
		}
		catch(Exception e){
		System.out.println("Der skete en fejl:  " + e);	
		}
		return i;
	}
	
	public ArrayList<Indlaeggelse> listAlleIndlaeggelser()
	{
		return listAlleIndlaeggelserMiscWhere("");
	}
	
	public ArrayList<Indlaeggelse> listAlleIndlaeggelserMiscWhere(String wClause) 
	{
		ResultSet results;
		ArrayList<Indlaeggelse> list = new ArrayList<Indlaeggelse>();

		String query = buildQuery(wClause);

		try 
		{
			Statement stmt = con.createStatement();
			stmt.setQueryTimeout(5);
			results = stmt.executeQuery(query);

			while (results.next()) 
			{
				Indlaeggelse iObj = new Indlaeggelse();
				iObj = buildIndlaeggelse(results);
				list.add(iObj);
			}// end while
			stmt.close();
			

		}// slut try
		catch (Exception e) 
		{
			System.out.println("Query exception - select: " + e);
			e.printStackTrace();
		}
		return list;
	}

}



